package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.FragmentStareAdapter;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.fragment.CarFragment;
import com.tianxin.workbao.workBao.fragment.CoalFragment;
import com.tianxin.workbao.workBao.fragment.CustomerFragment;
import com.tianxin.workbao.workBao.fragment.TestItemFragment;
import com.tianxin.workbao.workBao.holder.SearchViewHolder;

/**
 * Created by hacker on 2017/7/14.
 */

public class BaseDataActivity extends BaseActivity implements View.OnClickListener {

    private ViewPager mViewPage;
    private TabLayout mTabLayout;
    private FloatingActionButton mTopFab;
    private FragmentStareAdapter mFragmentStareAdapter;
    private int isFirstRefresh = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_data_activity);
        super.initStatusBar(false);

        initView();
        initEvent();
        initData();
        bindView();
    }

    private void initView() {
        mToolbar = findViewById(R.id.toolbar);
        mToolbar.setTitle(" ");
        mToolbar.setNavigationIcon(R.drawable.ic_action_back_24dp);
        setSupportActionBar(mToolbar);

        mViewPage = findViewById(R.id.base_data_viewPager);
        mViewPage.setOffscreenPageLimit(2);
        mViewPage.setCurrentItem(0);
        mTabLayout = findViewById(R.id.base_data_tabLayout);
        mTabLayout.setSelectedTabIndicatorColor(this.getResources().getColor(R.color.white));
        mTabLayout.setupWithViewPager(mViewPage);
        mTopFab = findViewById(R.id.base_data_top_fab);
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    @Override
    public void initData() {
        mTabTitles.add(0, getResources().getString(R.string.supply));
        mTabTitles.add(1, getResources().getString(R.string.customer));
        mTabTitles.add(2, getResources().getString(R.string.express));
        mTabTitles.add(3, getResources().getString(R.string.coal));
        mTabTitles.add(4, getResources().getString(R.string.car));
        mTabTitles.add(5, getResources().getString(R.string.testItem));

        mFragments.add(0, CustomerFragment.newInstance(Constant.CUSTOMER_TYPE_SALE));
        mFragments.add(1, CustomerFragment.newInstance(Constant.CUSTOMER_TYPE_BUY));
        mFragments.add(2, CustomerFragment.newInstance(Constant.CUSTOMER_TYPE_TRANS));
        mFragments.add(3, CoalFragment.newInstance());
        mFragments.add(4, CarFragment.newInstance());
        mFragments.add(5, TestItemFragment.newInstance());
    }

    @Override
    public void initEvent() {
        //navigationIcon event
        mToolbar.setNavigationOnClickListener(view -> BaseDataActivity.this.finish());
        mTopFab.setOnClickListener(this);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                searchTag = tab.getPosition();
                searchHint = tab.getText().toString();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void bindView() {
        mFragmentStareAdapter = new FragmentStareAdapter(getSupportFragmentManager(), mFragments, mTabTitles);
        mViewPage.setAdapter(mFragmentStareAdapter);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.base_data_top_fab:
                if (isFirstRefresh == 1) {
                    mFragments.get(searchTag).initData(false);
                } else {
                    isFirstRefresh = 0;
                }
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId) {
            case R.id.search:
                showSearchView(this, getSearchHandlerListener());
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    //搜索接口
    public SearchViewHolder.OnSearchHandlerListener getSearchHandlerListener() {
        if (searchHandlerListener == null) {
            searchHandlerListener = code -> {
                switch (code) {
                    case SearchViewHolder.RESULT_SEARCH_EMPTY_KEYWORD:
                        Snackbar.make(getWindow().getDecorView(),
                                R.string.keyword_is_empty,
                                Snackbar.LENGTH_SHORT).show();
                        break;
                    case SearchViewHolder.RESULT_SEARCH_SEARCH:
                        String query = mSearchViewHolder.et_search_content.getText().toString();
                        mFragments.get(searchTag).search(query);
                        break;
                    case SearchViewHolder.RESULT_SEARCH_GO_SCAN:

                        break;
                    case SearchViewHolder.RESULT_SEARCH_CANCEL:
                        mPopupWindow.dismiss();
                        break;
                }
            };
        }
        return searchHandlerListener;
    }
}
